//
//  CPSHHeroSelect.m
//  CardMoveGame
//
//  Created by peac on 2025/11/7.
//

#import "CPSHHeroSelect.h"

@interface CPSHHeroSelect ()
@property(nonatomic,strong) UIImageView *mdherobgIView;
@property(nonatomic,strong) UILabel *mdnameLab;
@property(nonatomic,strong) UILabel *mdlevelLab;
@property(nonatomic,strong) UIButton *mdpwrBtn;
@property(nonatomic,strong) UILabel *mdhpLab;
@property(nonatomic,strong) UILabel *mdatkLab;
@property(nonatomic,strong) UILabel *mddefLab;
@property(nonatomic,strong) UIButton *mdgoldBtn;
@property(nonatomic,strong) UIButton *mdjinyanBtn;
@property(nonatomic,assign) NSInteger heroSelect;
@end

@implementation CPSHHeroSelect

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSUserDefaults *mduserDefault=[NSUserDefaults standardUserDefaults];
    for (int i=0; i<4; i++) {
        if ([mduserDefault integerForKey:[NSString stringWithFormat:@"baokemlevel%d",i]]==0) {
            [mduserDefault setInteger:1 forKey:[NSString stringWithFormat:@"baokemlevel%d",i]];
        }
        
        
        
    }
    
    
    
    self.heroSelect=0;
    self.mdherobgIView=[[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.mdherobgIView];
    self.mdherobgIView.image=[UIImage imageNamed:@"mdheroimage0"];
    
  
    
    self.mdnameLab=[[UILabel alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height*0.15, self.view.frame.size.width, self.view.frame.size.height*0.1) ];
    self.mdnameLab.textAlignment=NSTextAlignmentCenter;
    self.mdnameLab.font=[UIFont boldSystemFontOfSize:30];
    [self.view addSubview:self.mdnameLab];
    self.mdnameLab.textColor=[UIColor whiteColor];
    self.mdnameLab.text=@"dddddddd";
    
    
    self.mdlevelLab=[[UILabel alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height*0.20, self.view.frame.size.width, self.view.frame.size.height*0.1) ];
    self.mdlevelLab.textAlignment=NSTextAlignmentRight;
    self.mdlevelLab.font=[UIFont boldSystemFontOfSize:26];
    self.mdlevelLab.textColor=[UIColor whiteColor];
    [self.view addSubview:self.mdlevelLab];
    self.mdlevelLab.text=@"dddddddd";
    self.mdpwrBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.mdpwrBtn.frame=CGRectMake(self.view.frame.size.width-self.view.frame.size.height*0.09*2.27, self.view.frame.size.height*0.25, self.view.frame.size.height*0.09*2.27, self.view.frame.size.height*0.09);
    [self.mdpwrBtn setBackgroundImage:[UIImage imageNamed:@"pwrbtnimage"] forState:UIControlStateNormal];
    [self.view addSubview:self.mdpwrBtn];
    
    
    UIView *detailView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height*0.65, self.view.frame.size.width,self.view.frame.size.height*0.35)];
    detailView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.7];
    [self.view addSubview:detailView];
    
    self.mdhpLab=[[UILabel alloc]initWithFrame:CGRectMake(0, detailView.frame.size.height*0.07, self.view.frame.size.width/3, detailView.frame.size.height*0.1) ];
    self.mdhpLab.textAlignment=NSTextAlignmentCenter;
    self.mdhpLab.font=[UIFont boldSystemFontOfSize:30];
    [detailView addSubview:self.mdhpLab];
    self.mdhpLab.textColor=[UIColor whiteColor];
    self.mdhpLab.text=@"999";
    
    self.mdatkLab=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3, detailView.frame.size.height*0.07, self.view.frame.size.width/3, detailView.frame.size.height*0.1) ];
    self.mdatkLab.textAlignment=NSTextAlignmentCenter;
    self.mdatkLab.font=[UIFont boldSystemFontOfSize:30];
    [detailView addSubview:self.mdatkLab];
    self.mdatkLab.textColor=[UIColor whiteColor];
    self.mdatkLab.text=@"999";
    
    self.mddefLab=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3*2, detailView.frame.size.height*0.07, self.view.frame.size.width/3, detailView.frame.size.height*0.1) ];
    self.mddefLab.textAlignment=NSTextAlignmentCenter;
    self.mddefLab.font=[UIFont boldSystemFontOfSize:30];
    [detailView addSubview:self.mddefLab];
    self.mddefLab.textColor=[UIColor whiteColor];
    self.mddefLab.text=@"999";
    
    self.mdgoldBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.mdgoldBtn.frame=CGRectMake(self.view.frame.size.width/2-10-self.view.frame.size.height*0.067*2.96, detailView.frame.size.height*0.3-self.view.frame.size.height*0.067/2, self.view.frame.size.height*0.067*2.96,self.view.frame.size.height*0.067);
    [self.mdgoldBtn setBackgroundImage:[UIImage imageNamed:@"金币"] forState:UIControlStateNormal];
    [detailView addSubview:self.mdgoldBtn];
    self.mdgoldBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 30, 0, 0);
    
    self.mdjinyanBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.mdjinyanBtn.frame=CGRectMake(self.view.frame.size.width/2+10, detailView.frame.size.height*0.3-self.view.frame.size.height*0.09/2, self.view.frame.size.height*0.09*1.98, self.view.frame.size.height*0.09);
    [self.mdjinyanBtn setBackgroundImage:[UIImage imageNamed:@"经验"] forState:UIControlStateNormal];
    [detailView addSubview:self.mdjinyanBtn];
    self.mdjinyanBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 30, 0, 0);
    UIButton  *mdupgradeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    mdupgradeBtn.frame=CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.067*2.78/2, detailView.frame.size.height*0.47, self.view.frame.size.height*0.067*2.78, self.view.frame.size.height*0.067);
    [mdupgradeBtn setBackgroundImage:[UIImage imageNamed:@"Upgrade"] forState:UIControlStateNormal];
    [detailView addSubview:mdupgradeBtn];
    [mdupgradeBtn addTarget:self action:@selector(mdupgradeBtnclicked) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *mdleftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    mdleftBtn.frame=CGRectMake(20,self.view.frame.size.height*0.47, self.view.frame.size.height*0.06*0.9, self.view.frame.size.height*0.06);
    [mdleftBtn setBackgroundImage:[UIImage imageNamed:@"左"] forState:UIControlStateNormal];
    [mdleftBtn addTarget:self action:@selector(mdleftBtnclicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mdleftBtn];
    
    UIButton *mdrightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    mdrightBtn.frame=CGRectMake(self.view.frame.size.width-20-self.view.frame.size.height*0.06*0.9,self.view.frame.size.height*0.47, self.view.frame.size.height*0.06*0.9, self.view.frame.size.height*0.06);
    [mdrightBtn setBackgroundImage:[UIImage imageNamed:@"右"] forState:UIControlStateNormal];
    [mdrightBtn addTarget:self action:@selector(mdrightBtnclicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mdrightBtn];
    
    
    [self setbaokemengdate];
}
-(void)setbaokemengdate{
    NSArray *nameArr=@[@"Gardevoir",@"Dragonite",@"Moltres",@"Lapras"];
    self.mdherobgIView.image=[UIImage imageNamed:[NSString stringWithFormat:@"角色%ld",(long)self.heroSelect]];
    
    self.mdnameLab.text=nameArr[self.heroSelect];
    NSInteger level=(long)[[NSUserDefaults standardUserDefaults]integerForKey:[NSString stringWithFormat:@"baokemlevel%ld",(long)self.heroSelect]];
    
    self.mdlevelLab.text=[NSString stringWithFormat:@"LV.%ld",(long)level];
    
    [self.mdpwrBtn setTitle:[NSString stringWithFormat:@"%ld",(long)(4000+1000*level)] forState:UIControlStateNormal];
    self.mdhpLab.text=[NSString stringWithFormat:@"HP:%ld",(long)(300+200*level)];
    self.mdatkLab.text=[NSString stringWithFormat:@"ATK:%ld",(long)(80+level*20)];
    self.mddefLab.text=[NSString stringWithFormat:@"DEF:%ld",(long)(10+level*10)];
    [self.mdgoldBtn setTitle:[NSString stringWithFormat:@"%ld/%ld",(long)[[NSUserDefaults standardUserDefaults]integerForKey:@"mdgold"],(long)(30+10*level)] forState:UIControlStateNormal];
    
    [self.mdjinyanBtn setTitle:[NSString stringWithFormat:@"%ld/%ld",(long)[[NSUserDefaults standardUserDefaults]integerForKey:@"mdjingyan"],(long)(400+100*level)] forState:UIControlStateNormal];
    
}

-(void)mdleftBtnclicked{
    if (self.heroSelect==0) {
        self.heroSelect=3;
    }else{
        self.heroSelect--;
        
        
    }
    
    [self setbaokemengdate];
    
    
}
-(void)mdrightBtnclicked{
    
    if (self.heroSelect==3) {
        self.heroSelect=0;
    }else{
        self.heroSelect++;
        
        
    }
    
    [self setbaokemengdate];
}

-(void)mdupgradeBtnclicked{
    NSInteger level=(long)[[NSUserDefaults standardUserDefaults]integerForKey:[NSString stringWithFormat:@"baokemlevel%ld",(long)self.heroSelect]];
    if ((long)[[NSUserDefaults standardUserDefaults]integerForKey:@"mdgold"]<(30+10*level)||(long)[[NSUserDefaults standardUserDefaults]integerForKey:@"mdjingyan"]<(400+100*level)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tips"
                                                                     message:@"Not enough item"
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *restartAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
        }];
    
        [alert addAction:restartAction];
        [self presentViewController:alert animated:YES completion:nil];
        
        
        return;
    }
    NSUserDefaults  *userde=[NSUserDefaults standardUserDefaults];
    
    [userde setInteger:[userde integerForKey:@"mdgold"]-(30+10*level) forKey:@"mdgold"];
    [userde setInteger:[userde integerForKey:@"mdjingyan"]-(400+100*level) forKey:@"mdjingyan"];
    [userde setInteger:level+1 forKey:[NSString stringWithFormat:@"baokemlevel%ld",(long)self.heroSelect]];
    [self setbaokemengdate];
}

@end
